/*
 * AddTema.java
 *
 * Created on 2 de diciembre de 2008, 11:31
 */
package Vistas;

import Dominio.CtrlDominio;
import Dominio.Strouble;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author  sandra
 */
public class AddTema extends javax.swing.JFrame {

    private CtrlDominio CD = CtrlDominio.getInstance();
    private Vector<Strouble> temasPos = new Vector<Strouble>(0, 1);
    private Vector<Strouble> temasSeg = new Vector<Strouble>(0, 1);
    private Vector<Strouble> temas = new Vector<Strouble>(0, 1);
    private static String[] cabecerasTablaTema = {"Temas", "Coincidencias"};
    private NotManual not;

    /** Creates new form AddTema */
    /* public AddTema(int i, AuxNotManual auxx) {
    initComponents();
    }*/
    public AddTema(Vector<Strouble> temasV, Vector<Strouble> tPos, Vector<Strouble> tSeg, NotManual not2) {
        initComponents();
        not = not2;
        temas = temasV;
        temasPos = tPos;
        temasSeg = tSeg;
        this.setTitle("A\u00f1adir Tema");
        this.setLocationRelativeTo(null);
        if (temas.size() == 0) {
            JOptionPane.showMessageDialog(this, "No es posible mostrar los temas, porque no hay ning\u00fan tema en el diccionario", "No hay temas en el diccionario", JOptionPane.INFORMATION_MESSAGE);
            Object[][] aux2 = new Object[temas.size()][2];
            for (int i = 0; i < temas.size(); i++) {
                aux2[i][0] = temas.get(i).nom;
                aux2[i][1] = temas.get(i).dist;
            }
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux2, cabecerasTablaTema));
        } else {
            Object[][] aux2 = new Object[temas.size()][2];
            for (int i = 0; i < temas.size(); i++) {
                aux2[i][0] = temas.get(i).nom;
                aux2[i][1] = temas.get(i).dist;
            }
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux2, cabecerasTablaTema));
            String fs = System.getProperty("file.separator");
            ImageIcon ic = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "add.png");
            jAddList.setIcon(ic);
            jAddTema.setIcon(ic);
            ImageIcon ic2 = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "cancel.png");
            jButton3.setIcon(ic2);
            
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTemas = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jNuevoTema = new javax.swing.JTextField();
        jAddTema = new javax.swing.JButton();
        jAddList = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTemas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Temas", "Coincidencia"
            }
        ));
        jScrollPane1.setViewportView(jTemas);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jAddTema.setText("Añadir Nuevo Tema");
        jAddTema.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jAddTemaMouseClicked(evt);
            }
        });

        jAddList.setText("Añadir Tema de la lista");
        jAddList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jAddListMouseClicked(evt);
            }
        });

        jButton3.setText("Cancelar");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jAddList, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addComponent(jNuevoTema, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addComponent(jAddTema, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jAddList)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(jNuevoTema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jAddTema)
                .addGap(49, 49, 49)
                .addComponent(jButton3)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jAddListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jAddListMouseClicked
        int fila = jTemas.getSelectedRow();
        if (fila == -1) {
            JOptionPane.showMessageDialog(this, "Selecciona un tema para consultar", "No se ha seleccionado un tema de la lista", JOptionPane.INFORMATION_MESSAGE);
        } else {
            Strouble temaS = temas.get(fila);
            if (temasSeg.contains(temaS)) {
                JOptionPane.showMessageDialog(this, "El tema ya est\u00e1 a\u00f1adido a la noticia", "Tema a\u00f1adido", JOptionPane.INFORMATION_MESSAGE);
            } else if (temasPos.contains(temaS)) {
                temasPos.remove(temaS);
                temasSeg.add(temaS);
                not.actualizarTablas();
                not.activarBotones();
                this.dispose();
            } else {
                temasSeg.add(temaS);
                not.actualizarTablas();
                not.activarBotones();
                this.dispose();
            }
        }        
    }//GEN-LAST:event_jAddListMouseClicked

    private void jAddTemaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jAddTemaMouseClicked
        Strouble temaS = new Strouble();
        temaS.dist = 0;
        temaS.nom = jNuevoTema.getText();
        if (temaS.nom.isEmpty()) {
            JOptionPane.showMessageDialog(this, "No hay ning\u00fan tema nuevo para a\u00f1adir, int\u00e9ntelo de nuevo.", "No hay tema nuevo", JOptionPane.INFORMATION_MESSAGE);
        } else {

            if (temaS.nom.contains("  ") || temaS.nom.endsWith(" ") || temaS.nom.startsWith(" ")) {
                JOptionPane.showMessageDialog(this, "El tema que intentas a\u00f1adir est\u00e1 en formato incorrecto", "Tema incorrecto", JOptionPane.INFORMATION_MESSAGE);
            } else if (CD.estaDiccionarioTemas(temaS.nom)) {
                JOptionPane.showMessageDialog(this, "El tema que intentas a\u00f1adir ya est\u00e1 en el diccionario.", "Ya existe el tema", JOptionPane.INFORMATION_MESSAGE);
            } else {
                temasSeg.add(temaS);
                not.activarBotones();
                not.actualizarTablas();
                this.dispose();
            }
        }
}//GEN-LAST:event_jAddTemaMouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        not.activarBotones();
        this.dispose();
    }//GEN-LAST:event_jButton3MouseClicked
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAddList;
    private javax.swing.JButton jAddTema;
    private javax.swing.JButton jButton3;
    private javax.swing.JTextField jNuevoTema;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTemas;
    // End of variables declaration//GEN-END:variables
}
